package cn.edu.xmu.experiment.PulseComms.Service;


import cn.edu.xmu.experiment.PulseComms.entity.GroupChat;
import cn.edu.xmu.experiment.PulseComms.entity.Message;
import cn.edu.xmu.experiment.PulseComms.repository.GroupChatRepository;
import cn.edu.xmu.experiment.PulseComms.repository.MessageRepository;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MessageRepository messageRepository;


    public static final String PRIVATE_EXCHANGE = "private.exchange";

    public void sendPrivateMessage(Message message) {
        rabbitTemplate.convertAndSend(PRIVATE_EXCHANGE, message.getReceiver(), message);
        messageRepository.save(message);
    }

    public List<Message> getMessagesByUser(String username) {
        return messageRepository.findPrivateMessagesByUsername(username);
    }

    public List<Message> getGroupMessages(String groupName) {
        return messageRepository.findByGroupName(groupName);
    }

}    